﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DataAccess
{
    public class DAAdmins
    {
        /// <summary>
        /// Get admin
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Admin GetAdmin(string username, string password)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    var temp = db.Admins.FirstOrDefault(o => o.Username == username && o.Password == password);
                    return temp;
                }
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// Update profile
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public bool Update(Admin admin)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    var temp = db.Admins.FirstOrDefault(o => o.Id == admin.Id);
                    if (temp == null)
                        return false;
                    temp.Username = admin.Username;
                    temp.Firstname = admin.Firstname;
                    temp.Lastname = admin.Lastname;
                    temp.Password = admin.Password == null ? temp.Password : admin.Password;
                    temp.Jobtitle = admin.Jobtitle;
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public object GetAdmins(int pageIndex, int pageSize, out int totalPages)
        {
            totalPages = 0;
            try
            {
                int startIndex = pageIndex * pageSize;
                using (var db = new DBWSSDataContext())
                {
                    IQueryable<Admin> temp = db.Admins;
                    if (temp.Count() == 0) return null;
                    temp = temp.OrderBy(o => o.Username).Skip(startIndex).Take(pageSize);
                    int totalRecords = temp.Count();
                    totalPages = totalRecords / pageSize * pageSize == totalRecords ? totalRecords / pageSize : totalRecords / pageSize + 1;
                    return temp.ToList();
                }
            }
            catch
            {
                return null;
            }
        }

        public Admin GetAdmin(int adminId)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    return db.Admins.FirstOrDefault(o => o.Id == adminId);
                }
            }
            catch
            {
                return null;
            }
        }

        public bool Insert(Admin admin)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    db.Admins.InsertOnSubmit(admin);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        public bool Delete(int adminId)
        {
            try
            {
                using (var db = new DBWSSDataContext())
                {
                    var temp = db.Admins.FirstOrDefault(o => o.Id == adminId);
                    if (temp == null) return false;
                    db.Admins.DeleteOnSubmit(temp);
                    db.SubmitChanges();
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}